<?php

namespace app\staff\controller;

use app\common\controller\Api;
use app\common\model\Staff as StaffModel;
use app\common\controller\StaffApi;
use app\common\library\Email;
use app\common\model\Order as OrderModel;
use app\common\model\Stat;
use app\common\model\StoreProject;
use app\common\model\Suborder;
use think\Db;
use think\Exception;


/**
 * 订单接口
 */
class Order extends Api
{

    protected $noNeedRight = ['*'];

    //统计
    public function data()
    {
        $date = input('date','');

        $start_date = empty($date) ? strtotime(date('Y-m', strtotime("today"))) : strtotime($date);
        $end_date = strtotime("+1 month", $start_date);

        $stat_model = new Stat();

        $num = $stat_model->projectNum($this->auth->id,$start_date,$end_date);

        $total_hour = $stat_model->orderHour($this->auth->id,$start_date,$end_date);

        $pay_price = $stat_model->orderPayPrice($this->auth->id,$start_date,$end_date);

        $data = [
            'num'=>$num,
            'hour'=>$total_hour,
            'pay_price'=>$pay_price
        ];

        $this->success('',$data);
    }

    //订单列表
    public function lists()
    {
        $where = ['o.staff_id'=>$this->auth->id,'o.is_hang'=>0];

        $date = input('date','');

        if ($date){
            $start_date = empty($date) ? strtotime(date('Y-m', strtotime("today"))) : strtotime($date);
            $end_date = strtotime("+1 month", $start_date);
            $where['o.date'] = ['between', [date('Y-m-d H:i:s',$start_date), date('Y-m-d H:i:s',$end_date)]];
        }

        $order_model = new OrderModel();

        $list = $order_model->lists($where);

        $this->success('',$list);
    }

    //订单详情
    public function info()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');
        $order_model = new OrderModel();

        $res = $order_model->info($order_id);

        $this->success('',$res);
    }


    //根据日期查订单列表视图
    public function date()
    {

        $date = input('date','');
        if (!$date)$this->error('缺少参数日期');
        $keyword = input('keyword','');
        // 假设你要获取今天的时间范围

        $where = ['o.staff_id'=>$this->auth->id,'o.is_end'=>0];
        if ($keyword){
            $where['o.last_name|o.first_name'] = ['like',"%$keyword%"];
        }

        $order_model = new OrderModel();
        $list = $order_model->dateorder($where,$date);
        $this->success('',$list);
    }

    //根据日期查订单列表日历视图
    public function datetime()
    {

        $date = input('date','');
        if (!$date)$this->error('缺少参数日期');

        $where = ['o.staff_id'=>$this->auth->id,'o.is_end'=>0];
        $order_model = new OrderModel();

        $list = $order_model->datetimeorder($where,$date);

        $this->success('',$list);
    }

    //上钟
    public function start()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');
        if ($info->is_start == 1)$this->error('已上钟');
        Db::startTrans();
        try {
            $info->is_start = 1;
            $info->start_time = time();
            $info->order_status = 1;
            $info->order_status_name = '服务中';
            $info->save();

            //记录下钟时间
            $staff_model = new \app\common\model\User();
            $staff_model->editServiceStatus($order_id,$this->auth->id);
            if ($info->email){
                $message_title = \app\common\model\Config::get(['name'=>'start_title'])->value;
                $message_content = \app\common\model\Config::get(['name'=>'start_content'])->value;
                //$message_title = '订单编号:'.$info['order_no'].'技师'.$this->auth->nickname.'已上钟';
                //$message_content = '订单编号:'.$info['order_no'].'技师'.$this->auth->nickname.'已上钟';
                $email = new Email;
                $result = $email
                    ->to($info->email)
                    ->subject($message_title)
                    ->message($message_content)
                    ->send();

                if (!$result) {
                    Db::rollback();
                    throw new Exception($email->getError());
                }
            }


            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');

    }

    //下钟
    public function end()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');
        if ($info->is_end == 1)$this->error('已下钟');
        Db::startTrans();
        try {
            $info->is_end = 1;
            $info->end_time = time();
            $info->order_status = 2;
            $info->order_status_name = '已完成';

            $info->save();

            $staff_model = new \app\common\model\User();
            $staff_model->edit($this->auth->id,0);
            if($info->email){
                $message_title = \app\common\model\Config::get(['name'=>'end_title'])->value;
                $message_content = \app\common\model\Config::get(['name'=>'end_content'])->value;

                $email = new Email;
                $result = $email
                    ->to($info->email)
                    ->subject($message_title)
                    ->message($message_content)
                    ->send();

                if (!$result) {
                    Db::rollback();
                    throw new Exception('邮箱发送失败,邮箱错误');
                }
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');

    }

    //加钟
    public function add()
    {
        $order_id = input('order_id',0);
        $project_id = input('project_id',0);
        $num = input('num',0);
        if (!$project_id)$this->error('请选择服务项目');
        if (!$num)$this->error('请选择服务项目数量');
        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');

        $suborder_model = new Suborder();

        $data = [
            'order_id'=>$order_id,
            'staff_id'=>$this->auth->id,
            'project_id'=>$project_id,
            'num'=>$num
        ];

        $res = $suborder_model->save($data);

        if ($res){
            //下钟时间增加
            $staff_model = new \app\common\model\User();
            $staff_model->editWorkEndTime($order_id,$project_id,$num,$this->auth->id);
            $this->success('加钟成功');
        } else {
            $this->error('加钟失败');
        }
    }

    //订单支付的项目金额/数量
    public function paydata()
    {
        $order_id = input('order_id',0);

        $order_model = new OrderModel();

        $info = $order_model->paydata($order_id);

        $this->success('',$info);
    }


    //支付
    public function pay()
    {
        $order_id = input('order_id',0);
        $cash_price = input('cash_price',0);
        $gift_card_tip_price = input('gift_card_tip_price',0);
        $swiping_card_price = input('swiping_card_price',0);
        $swiping_card_tip_price = input('swiping_card_tip_price',0);
        $gift_card_price = input('gift_card_price',0);

        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');

        $order_model = new OrderModel();

        $res = $order_model->pay($order_id,$cash_price,$gift_card_tip_price,$swiping_card_price,$swiping_card_tip_price,$gift_card_price);

        $this->success('支付成功',$res);
    }

    //挂单
    public function hang()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get(['order_id'=>$order_id,'staff_id'=>$this->auth->id]);
        if (!$info)$this->error('订单不存在');

        $info->is_hang = 1;

        if ($info->save()){

            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    //挂单列表
    public function hangList()
    {
        $where = ['o.staff_id'=>$this->auth->id,'o.is_hang'=>1];

        $order_model = new OrderModel();

        $list = $order_model->lists($where);

        $this->success('',$list);

    }


    //快捷开单
    public function fastOpen()
    {
        $param = input();

        $order_model = new OrderModel();

        $list = $order_model->staffFastOpenOrder($param,$this->auth->id);

        $this->success('',$list);
    }

    //抢单大厅
    public function robOrder()
    {
        $search_type = input('search_type',0);

        $keyword = input('keyword','');

        //$where = ['o.store_id'=>$store_id];
        $where = ['staff_id'=>0,'allot_time'=>0,'is_cancel'=>0];

        if ($keyword){
            if ($search_type==0){
                //订单搜索
                $where['o.last_name|o.first_name|o.order_no|o.phone|o.email'] = ['like',"%$keyword%"];
            } elseif ($search_type==1){
                //项目搜索
                $where['sp.project_name'] = ['like',"%$keyword%"];
            } elseif ($search_type==2){
                //门店搜索
                $where['s.store_name'] = ['like',"%$keyword%"];
            } elseif ($search_type==3){
                //技师搜索
                $where['su.nickname'] = ['like',"%$keyword%"];
            }
        }

        $order_model = new OrderModel();

        $list = $order_model->lists($where);

        $this->success('',$list);
    }

    //抢单
    public function rob()
    {
        $order_id = input('order_id',0);;

        $info = OrderModel::where(['order_id'=>$order_id])->lock(true)->find();
        if (!$info)$this->error('订单不存在');
        if ($info->staff_id!=0)$this->error('该单已失效');
        $info->staff_id = $this->auth->id;
        $info->allot_time = time();

        if ($info->save()){
            $staff_model = new \app\common\model\User();
            $staff_model->edit($this->auth->id,3);
            $this->success('抢单成功');
        } else {
            $this->error('抢单失败');
        }
    }

}
